home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / generic / rshift.c < prev    next >
C/C++ Source or Header  |  1994-04-26  |  2KB  |  88 lines

  1. /* __mpn_rshift -- Shift right a low-level natural-number integer.
  2.  
  3. Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. This file is part of the GNU MP Library.
  6.  
  7. The GNU MP Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Library General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11.  
  12. The GNU MP Library is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  15. License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public License
  18. along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
  19. the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include "gmp.h"
  22. #include "gmp-impl.h"
  23.  
  24. /* Shift U (pointed to by UP and USIZE limbs long) CNT bits to the right
  25.    and store the USIZE least significant limbs of the result at WP.
  26.    The bits shifted out to the right are returned.
  27.  
  28.    Argument constraints:
  29.    1. 0 < CNT < BITS_PER_MP_LIMB
  30.    2. If the result is to be written over the input, WP must be <= UP.
  31. */
  32.  
  33. mp_limb
  34. #if __STDC__
  35. __mpn_rshift (register mp_ptr wp,
  36.         register mp_srcptr up, mp_size_t usize,
  37.         register unsigned int cnt)
  38. #else
  39. __mpn_rshift (wp, up, usize, cnt)
  40.      register mp_ptr wp;
  41.      register mp_srcptr up;
  42.      mp_size_t usize;
  43.      register unsigned int cnt;
  44. #endif
  45. {
  46.   register mp_limb high_limb, low_limb;
  47.   register unsigned sh_1, sh_2;
  48.   register mp_size_t i;
  49.   mp_limb retval;
  50.  
  51. #ifdef DEBUG
  52.   if (usize == 0 || cnt == 0)
  53.     abort ();
  54. #endif
  55.  
  56.   sh_1 = cnt;
  57.  
  58. #if 0
  59.   if (sh_1 == 0)
  60.     {
  61.       if (wp != up)
  62.     {
  63.       /* Copy from low end to high end, to allow specified input/output
  64.          overlapping.  */
  65.       for (i = 0; i < usize; i++)
  66.         wp[i] = up[i];
  67.     }
  68.       return usize;
  69.     }
  70. #endif
  71.  
  72.   wp -= 1;
  73.   sh_2 = BITS_PER_MP_LIMB - sh_1;
  74.   high_limb = up[0];
  75.   retval = high_limb << sh_2;
  76.   low_limb = high_limb;
  77.  
  78.   for (i = 1; i < usize; i++)
  79.     {
  80.       high_limb = up[i];
  81.       wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
  82.       low_limb = high_limb;
  83.     }
  84.   wp[i] = low_limb >> sh_1;
  85.  
  86.   return retval;
  87. }
  88.